/*
 * Copyright (c) 2008-2009 Bill Whitacre http://rampancy.g0dsoft.com
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
*/

#ifndef RAT_PHYSICS_QUAD_TREE
#define RAT_PHYSICS_QUAD_TREE

#include "common.h"
#include "aabb.h"
#include "body.h"

struct rat_quad_tree_node;
struct rat_quad_tree;

typedef void (*rat_quad_tree_each_method)(rat_body *a,rat_body *b,void *data);

typedef struct rat_quad_tree_node
{
	struct rat_quad_tree *tree;
	struct rat_quad_tree_node *parent;

	rat_body **thisnode_entities;
	rat_body **childnode_entities;
	unsigned int numentities;
	unsigned int numchildents;

	int are_children;
	struct rat_quad_tree_node **children; // always four children if at all
	unsigned int depth;

	aabb quad;
} rat_quad_tree_node;

typedef struct rat_quad_tree
{
	rat_quad_tree_node *root;

	unsigned int numnodes;
	unsigned int maxdepth;

	int is_built;
} rat_quad_tree;

typedef struct rat_quad_tree_matrix
{
	rat_quad_tree **quadtrees;
	vector2 origin,cellsize;
	unsigned int matrix_width,matrix_height,numcells;
} rat_quad_tree_matrix;

#ifdef __cplusplus
extern "C" {
#endif

// stuff for the quad tree matrix which will
// be used by the body manager.

rat_quad_tree_matrix *rat_quad_tree_matrix_create(vector2 origin,vector2 cellsize,unsigned int numcells_x,unsigned int numcells_y);
void rat_quad_tree_matrix_destroy(rat_quad_tree_matrix *matrix);

void rat_quad_tree_matrix_build(rat_quad_tree_matrix *matrix,rat_body **entities,unsigned int numentities);
void rat_quad_tree_matrix_eachin(rat_quad_tree_matrix *matrix,rat_quad_tree_each_method each_method,void *data);
void rat_quad_tree_matrix_clear(rat_quad_tree_matrix *matrix);

// quad tree stuff.

rat_quad_tree_node *rat_quad_tree_node_create(aabb quad,unsigned int depth,rat_quad_tree *tree,rat_quad_tree_node *parent);
rat_quad_tree *rat_quad_tree_create(aabb quad);

void rat_quad_tree_node_destroy(rat_quad_tree_node *node);
void rat_quad_tree_destroy(rat_quad_tree *tree);

int rat_quad_tree_node_query_entity(rat_quad_tree_node *node,rat_body *entity);
void rat_quad_tree_node_split(rat_quad_tree_node *node);

void rat_quad_tree_build(rat_quad_tree *tree,rat_body **entities,unsigned int numentities);
//void rat_quad_tree_draw(rat_quad_tree *tree);

void rat_quad_tree_node_eachin(rat_quad_tree_node *node,rat_quad_tree_each_method each_method,void *data);
void rat_quad_tree_eachin(rat_quad_tree *tree,rat_quad_tree_each_method each_method,void *data);

void rat_quad_tree_node_clear(rat_quad_tree_node *node);
void rat_quad_tree_clear(rat_quad_tree *tree);

#ifdef __cplusplus
}
#endif

#endif
